﻿using EasyInvoice.WPF.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Unity;
using appCtx = EasyInvoice.ApplicationContext.AppContext;
using FirstFloor.ModernUI.Presentation;
using System.Windows;
using System.Windows.Data;

namespace EasyInvoice.WPF
{
    public static class Repository
    {
      
        private static ObservableCollection<ClientViewModel> _allClients = GetAllClients();

        private static ObservableCollection<ClientViewModel> _allFilterClients = GetAllFilterClients();
        
        private static ObservableCollection<ClientViewModel> _sellers = null;

        public static ObservableCollection<ClientViewModel> Clients
        {
            get
            {

                return _allClients;
            }
            set
            {
                _allClients = value;
                
            }
        }

        public static ObservableCollection<ClientViewModel> Sellers
        {
            get
            {
                if (_sellers == null)
                    _sellers = GetSellers();
                return _sellers;
            }
            set
            {
                _sellers = value;
                
            }
        }

        public static ObservableCollection<ClientViewModel> GetClients()
        {
            return _allClients;
        }
        public static ObservableCollection<ClientViewModel> GetFilterClients()
        {
            return _allFilterClients;
        }

        public static ObservableCollection<ClientViewModel> GetFilterSellers()
        {
            return new ObservableCollection<ClientViewModel>(from ClientViewModel client in _allFilterClients where client.IsMaster != false select client);
        }

        public static ObservableCollection<ClientViewModel> GetSellers()
        {
            return new ObservableCollection<ClientViewModel>(from ClientViewModel client in _allClients where client.IsMaster != false select client);
        }

        private static ObservableCollection<ClientViewModel> GetAllClients()
        {
            var returnValue = new ObservableCollection<ClientViewModel>();
            Services.ClientsService _serviceClients = appCtx.Instance.IoC.Resolve<Services.ClientsService>();
            var clients = _serviceClients.GetAll();
            foreach (Model.Client client in clients)
            {
                returnValue.Add(new ClientViewModel(client));
            }
            return returnValue;
        }

        private static ObservableCollection<ClientViewModel> GetAllFilterClients()
        {
            var returnValue = new ObservableCollection<ClientViewModel>();
            returnValue.Add(new ClientViewModel(new Model.Client() { Name = "(all)", IsMaster=true }));
            var clients = _allClients;
            clients.ToList().ForEach(i => returnValue.Add(i));
            return returnValue;
        }

        public static void Reset()
        {
            _sellers = null;
            _allClients = null;
            _allClients = GetAllClients();
            _allFilterClients = GetAllFilterClients();
            (Application.Current.FindResource("clientsComboDataSource") as ObjectDataProvider).Refresh();
            (Application.Current.FindResource("sellersComboDataSource") as ObjectDataProvider).Refresh();
            (Application.Current.FindResource("clientsFilterComboDataSource") as ObjectDataProvider).Refresh();
            (Application.Current.FindResource("sellersFilterComboDataSource") as ObjectDataProvider).Refresh();
        }

        #region InvoiceLinks
        private static LinkCollection _invoiceLinks = GetInvoiceLinks();

        public static LinkCollection InvoiceLinks
        {
            get
            {

                return _invoiceLinks;
            }
        }

        private static  LinkCollection GetInvoiceLinks()
        {

           
            var returnValue = new LinkCollection();
            Services.InvoicesService _serviceInvoices = appCtx.Instance.IoC.Resolve<Services.InvoicesService>();
            var invoices = _serviceInvoices.GetAll();

            foreach (Model.Invoice invoice in invoices)
            {
                returnValue.Add(new Link() 
                { 
                    DisplayName = invoice.Number + " from " + invoice.Date.ToString("yyyy MM dd"),
                    Source = new Uri(invoice.Id.ToString(), UriKind.Relative),
                
                });
            }
            return returnValue;


        }

        public static void ResetInvoices()
        {
            _invoiceLinks = null;
            _invoiceLinks = GetInvoiceLinks();
            //(Application.Current.FindResource("invoiceLinksDataSource") as ObjectDataProvider).Refresh();
        }
        #endregion

        //private static ObservableCollection<UserViewModel> _allUsers = GetAllUsers();
        //public static ObservableCollection<UserViewModel> Users
        //{
        //    get
        //    {

        //        return _allUsers;
        //    }
        //    set
        //    {
        //        _allUsers = value;
        //    }
        //}
        //private static ObservableCollection<UserViewModel> GetAllUsers()
        //{
        //    var returnValue = new ObservableCollection<UserViewModel>();
        //    var users = new ModelCE.User().GetAllCE();
        //    foreach (ModelCE.User user in users)
        //    {
        //        returnValue.Add(new UserViewModel(user));
        //    }
        //    return returnValue;
        //}
    }
}
